﻿using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;

namespace Sinbiota.Web.Controllers
{
    [AdminPermission]
    public class EcosystemController : BaseController
    {
        private readonly IEcosystemPersistenceServices _ecosystemPersistenceServices;

        public EcosystemController(IEcosystemPersistenceServices ecosystemPersistenceServices, IUserPersistenceServices userPersistenceServices)
        {
            _ecosystemPersistenceServices = ecosystemPersistenceServices;
        }

        //
        // GET: /Ecosystem/
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_ecosystemPersistenceServices.FindAll());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_ecosystemPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }


        //
        // GET: /Ecosystem/Details/5
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var ecosystem = _ecosystemPersistenceServices.FindById(id);
            return ecosystem == null ? View("NotFound") : View(ecosystem);
        }

        //
        // GET: /Ecosystem/Create
        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Ecosystem/Create
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(Ecosystem ecosystem)
        {
            var newEcosystem = new Ecosystem();
            TryUpdateModel(newEcosystem);
            if (ModelState.IsValid)
            {
                _ecosystemPersistenceServices.Save(newEcosystem);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(ecosystem);
        }

        //
        // GET: /Ecosystem/Edit/5
        [ImportMessagesFromTempData]
        public ActionResult Edit(int id)
        {
          var ecosystem = _ecosystemPersistenceServices.FindById(id);
            return ecosystem == null ? View("NotFound") : View(ecosystem);
        }

        //
        // POST: /Ecosystem/Edit/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, Ecosystem ecosystem)
        {
             var editEcosystem = _ecosystemPersistenceServices.FindById(id);
            TryUpdateModel(editEcosystem);
            if (ModelState.IsValid)
            {
                _ecosystemPersistenceServices.Update(editEcosystem);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(ecosystem);
        }

        //
        // GET: /Ecosystem/Delete/5
        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var ecosystem = _ecosystemPersistenceServices.FindById(id);
            return ecosystem == null ? View("NotFound") : View(ecosystem);
        }

        //
        // POST: /Ecosystem/Delete/5
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
           var deleteEcosystem = _ecosystemPersistenceServices.FindById(id);
            if (deleteEcosystem == null) return View("NotFound");
            _ecosystemPersistenceServices.Delete(deleteEcosystem);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
    }
}
